package com.hhsj.dynamicui.server.servlet.admin.consts;

import com.hhsj.androidserver.HttpServlet;
import com.hhsj.androidserver.HttpServletRequest;
import com.hhsj.androidserver.HttpServletResponse;
import com.hhsj.androidserver.WebServlet;
import com.hhsj.dynamicui.model.ConstantModel;
import com.hhsj.dynamicui.model.base.BaseModel;
import com.hhsj.dynamicui.server.DynamicUIService;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * 查询所有常量
 */
@WebServlet("/uhomecp-admin/const")
public class GetConstsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse rep) throws Exception {
        System.out.println("进入GetConstServlet");
        doPost(req, rep);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse rep) throws Exception {



        //JDBC 远程处理数据到 MySql 中
        String sql = "SELECT * FROM const;";
        PreparedStatement statement = DynamicUIService.getInstance().getConnection().prepareStatement(sql);
        ResultSet resultSet = statement.executeQuery();

        List<ConstantModel> constantModels = new ArrayList<>();
        while (resultSet.next()) {
            String const_id = String.valueOf(resultSet.getInt(1));
            String const_type = resultSet.getString(2);
            String const_code = resultSet.getString(3);
            String const_name = resultSet.getString(4);
            String const_desc = resultSet.getString(5);
            ConstantModel constantModel = new ConstantModel(const_id, const_type, const_code, const_name, const_desc);
            constantModels.add(constantModel);
        }


        //2.将数据封装到实体
        BaseModel<ConstantModel> baseModel = new BaseModel<>();
        baseModel.setDataList(constantModels);
        baseModel.setCode("0");
        baseModel.setMsg("成功");

        //3.返回响应
        rep.print(baseModel.toJSONString());
    }
}
